Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for Hash#each consistently yields a 2-element array #885

Closed

Conversation

JuanCrg90
Copy link
Contributor

From #823

https://bugs.ruby-lang.org/issues/12706?tab=history

As a semantics, Hash#each yields a 2-element array (pairs of keys and
values). So, { a: 1 }.each(&->(k, v) { }) should raise an exception
due to lambda's arity check.

https://bugs.ruby-lang.org/issues/12706?tab=history

> As a semantics, Hash#each yields a 2-element array (pairs of keys and
> values). So, { a: 1  }.each(&->(k, v) { }) should raise an exception
> due to lambda's arity check.
@JuanCrg90
Copy link
Contributor Author

What do you think @eregon ?


ruby_version_is "3.0" do
it 'consistently yields a 2-element array' do
-> { { a: 1 }.each(&->(k, v) { }) }.should raise_error(ArgumentError)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you assert the message too? I think it'd help clarity.
Also it'd be good to test that it works fine with a lambda taking a single argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW it seems the same change applies to Hash#each_pair too, so could you move it to the core/hash/shared/each.rb shared spec?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's already covered actually:

ruby_version_is "3.0" do
it "always yields an Array of 2 elements, even when given a callable of arity 2" do
obj = Object.new
def obj.foo(key, value)
end
-> {
{ "a" => 1 }.send(@method, &obj.method(:foo))
}.should raise_error(ArgumentError)
-> {
{ "a" => 1 }.send(@method, &-> key, value { })
}.should raise_error(ArgumentError)
end
end

@eregon
Copy link
Member

eregon commented Oct 27, 2021

Sorry for the late review. In the shared spec it seems already tested, so I'll close this.

@eregon eregon closed this Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants